package io.gitee.twoke.generator.spring.boot;

import io.gitee.twoke.generator.engine.GenerateEngine;
import io.gitee.twoke.generator.engine.GenerateService;
import io.gitee.twoke.generator.engine.MetaDataService;
import io.gitee.twoke.generator.engine.TemplateService;
import io.gitee.twoke.generator.engine.impl.generate.method.GlobalMethodManager;
import io.gitee.twoke.generator.engine.impl.generate.parameter.ParameterLoaderManager;
import io.gitee.twoke.generator.spring.GenerateEngineFactoryBean;
import io.gitee.twoke.generator.spring.SpringGenerateEngineConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public abstract class AbstractGenerateEngineAutoConfiguration
        extends AbstractGenerateEngineConfiguration {

  protected Set<Class<?>> getCustomMybatisMapperClasses(List<String> customMyBatisMappers) {
    Set<Class<?>> mybatisMappers = new HashSet<>();
    for (String customMybatisMapperClassName : customMyBatisMappers) {
      try {
        Class customMybatisClass = Class.forName(customMybatisMapperClassName);
        mybatisMappers.add(customMybatisClass);
      } catch (ClassNotFoundException e) {
        throw new IllegalArgumentException("Class " + customMybatisMapperClassName + " has not been found.", e);
      }
    }
    return mybatisMappers;
  }

  @Bean
  public GenerateEngineFactoryBean processEngine(SpringGenerateEngineConfiguration configuration) {
    return super.springProcessEngineBean(configuration);
  }

  @Bean
  @ConditionalOnMissingBean
  @Override
  public ParameterLoaderManager parameterLoaderManager(GenerateEngine generateEngine) {
    return super.parameterLoaderManager(generateEngine);
  }

  @Bean
  @ConditionalOnMissingBean
  @Override
  public GlobalMethodManager globalMethodManager(GenerateEngine generateEngine) {
    return super.globalMethodManager(generateEngine);
  }

  @Bean
  @ConditionalOnMissingBean
  @Override
  public GenerateService generateService(GenerateEngine generateEngine) {
    return super.generateService(generateEngine);
  }

  @Bean
  @ConditionalOnMissingBean
  @Override
  public TemplateService templateService(GenerateEngine generateEngine) {
    return super.templateService(generateEngine);
  }

  @Bean
  @ConditionalOnMissingBean
  @Override
  public MetaDataService metaDataService(GenerateEngine generateEngine) {
    return super.metaDataService(generateEngine);
  }
}